/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package program.gui;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.*;
import program.beans.Page;
import program.bl.PDFViewerController;

/**
 * A Panel where thumbnails of all PDF-Pages in PDFViewerController are shown
 * 
 * Responsible:  Bernhard
 */
public class ThumbnailPanel extends JScrollPane {

    public static final int ORDER_VERTICAL = 1;
    public static final int ORDER_HORIZONTAL = 2;
    private JPanel panel = new JPanel();
    private GridBagLayout gl;
    private int order;
    private PDFViewerController pdfvc = PDFViewerController.getInstance();
    private JPanel mainPanel;
    private int currentPage;
    
    /**
     * 
     * @param order
     * @param mainPanel 
     */
    public ThumbnailPanel(int order, JPanel mainPanel) {
        this.order = order;
        this.mainPanel = mainPanel;
        this.initComponents();
    }

    private void initComponents() {
        draw();
        this.setViewportView(panel);
    }

    /**
     * Draws thumbnails of the pages on the panel
     */
    public void draw() {
        ArrayList<Page> pages = (ArrayList<Page>) pdfvc.getAllPages(120, 120);
        panel.removeAll();
        if (order == ORDER_VERTICAL) {

            gl = new GridBagLayout();
            panel.setLayout(gl);
            GridBagConstraints gbc = null;

            for (int i = 0, j = 0; i < pages.size(); i++, j += 2) {
                gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = j;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                JLabel page = new JLabel("Seite: " + (i + 1), JLabel.LEFT);
                page.setName("" + (i + 1));
                page.addMouseListener(new LabelMouseAdapter());
                panel.add(page, gbc);
            }

            for (int i = 0, j = 1; i < pages.size(); i++, j += 2) {
                gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = j;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                JLabel page = new JLabel(new ImageIcon(pages.get(i).getImage()));
                page.setName("" + (i + 1));
                page.addMouseListener(new LabelMouseAdapter());
                panel.add(page, gbc);
            }

        } else {
            gl = new GridBagLayout();
            panel.setLayout(gl);
            GridBagConstraints gbc = null;

            for (int i = 0, j = 0; i < pages.size(); i++, j += 1) {
                gbc = new GridBagConstraints();
                gbc.gridx = j;
                gbc.gridy = 0;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.insets = new Insets(0, 10, 0, 0);
                JLabel page = new JLabel("Seite: " + (i + 1), JLabel.LEFT);
                page.setName("" + (i + 1));
                page.addMouseListener(new LabelMouseAdapter());
                panel.add(page, gbc);

                gbc = new GridBagConstraints();
                gbc.gridx = j;
                gbc.gridy = 1;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.insets = new Insets(0, 10, 0, 0);
                page = new JLabel(new ImageIcon(pages.get(i).getImage()));
                page.addMouseListener(new LabelMouseAdapter());
                page.setName("" + (i + 1));
                panel.add(page, gbc);
            }
        }
    }
//    public void addImage(ImageIcon ii) {
//        JLabel image = new JLabel(ii);
//        JLabel pageNumber = new JLabel("Seite " + (images.size() + 1));
//        image.setSize(60, 120);
//        images.add(image);
//
//        if (order == ORDER_VERTICAL) {
//            gl.setRows(gl.getRows() + 2);
//            panel.add(pageNumber);
//            panel.add(image);
//        } else {
//            gl.setColumns(gl.getColumns() + 1);
//        }
//
//        panel.setLayout(gl);
//
//
//
//
//    }

    public void markSelectedPage(int page) {
        for (Component c : panel.getComponents()) {
            if (c instanceof JLabel) {
                JLabel jl = (JLabel) c;
                jl.setBorder(null);
                if (jl.getName().equals("" + page)) {
                    if (jl.getIcon() == null) {
                        //Text label is showing the page number
                        jl.setBorder(BorderFactory.createMatteBorder(2, 2, 0, 2, Color.red));
                    } else {
                        //Label is the page itself
                        jl.setBorder(BorderFactory.createMatteBorder(0, 2, 2, 2, Color.red));
                    }
                }
            }
        }

        panel.updateUI();
    }

    class LabelMouseAdapter extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {
            int pageNumber = Integer.parseInt(e.getComponent().getName());
            markSelectedPage(pageNumber);
            currentPage = pageNumber;
            mainPanel.setLayout(new BorderLayout());
            mainPanel.removeAll();
            mainPanel.add(new JLabel(new ImageIcon(pdfvc.getPage(pageNumber - 1))));
            mainPanel.updateUI();
        }
    }

    public int getCurrentPage() {
        return currentPage;
    }
}
